package job

import (
	"gitee.com/crack007/goose"
	"gitee.com/crack007/goose/core/ws"
	"gitee.com/crack007/goose/schedule"
	"github.com/crack007/go-im/src/common/constant"
)

type StatJob struct {
	schedule.DefaultJob
	wsServer *ws.Server
}

func (s *StatJob) Run(task *schedule.Task) {
	total := s.wsServer.SessionManager.Count()
	user := 0
	s.wsServer.SessionManager.IterateSession(func(session *ws.Session) {
		if session.Tag == constant.SESSION_USER {
			user++
		}
	})
	goose.GetLogger().Info("在线用户统计,总共：%d  用户：%d", total, user)
}

func (s *StatJob) IsCron() bool {
	return false
}

func SubmitStatJob(delay int, wsServer *ws.Server) {
	schedule.SubmitJob(&StatJob{
		DefaultJob: schedule.DefaultJob{
			JobDelay: uint(delay),
			Id:       0,
			Name:     "ws.onlineStat",
		},
		wsServer: wsServer,
	})
}
